package com.chinaunicom.order.trigger.listener;

import com.alibaba.fastjson.JSON;
import com.chinaunicom.order.domain.model.entity.CancelOrderEntity;
import com.chinaunicom.order.domain.model.entity.OrderEntity;
import com.chinaunicom.order.domain.model.valobj.OrderStatus;
import com.chinaunicom.order.domain.repository.IOrderRepository;
import com.chinaunicom.order.domain.service.OrderAfterService;
import com.chinaunicom.order.types.PayOrderTimeoutMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author guomenghuai
 * @date 2024/1/15
 * @Description
 */
@Slf4j
@Component
public class PayOrderTimeoutListener implements MessageListenerConcurrently {

    @Resource
    private IOrderRepository orderRepository;

    @Resource
    private OrderAfterService orderAfterService;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        try {
            for (MessageExt messageExt : list) {

                String message = new String(messageExt.getBody());
                log.info("PayOrderTimeoutListener---收到超时未支付消息：{}", message);
                PayOrderTimeoutMessage payOrderTimeoutMessage = JSON.parseObject(message, PayOrderTimeoutMessage.class);

                CancelOrderEntity cancelOrderEntity = new CancelOrderEntity();
                cancelOrderEntity.setOrderId(payOrderTimeoutMessage.getOrderId());
                cancelOrderEntity.setUserId(payOrderTimeoutMessage.getUserId());
                cancelOrderEntity.setOrderStatus(payOrderTimeoutMessage.getOrderStatus());

                OrderEntity orderEntity = orderRepository.getByOrderId(payOrderTimeoutMessage.getOrderId());
                if (Objects.isNull(orderEntity)) {
                    log.warn("订单不存在，orderId：{}", cancelOrderEntity.getOrderId());
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }

                OrderStatus orderStatus = orderEntity.getOrderStatus();
                if (!Objects.equals(OrderStatus.CREATED.getCode(), orderStatus.getCode())) {
                    //  订单实时状态不等于已创建
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }

                orderAfterService.cancelOrder(cancelOrderEntity);
                log.info("PayOrderTimeoutListener---关闭订单，orderId：{}", cancelOrderEntity.getOrderId());
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            log.error("consumer error", e);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    }
}
